/*
 * SongInfoPanel.java
 *
 * Created on September 19, 2005, 3:04 PM
 */
package com.bff.slimserver.musicjukebox.ui;

import com.bff.bjj.ui.component.ColorProperties;
import com.bff.slimserver.musicjukebox.lib.JukeboxProperties;
import com.bff.slimserver.musicjukebox.ui.components.CoverArtThumbnailDialog;
import com.bff.bjj.ui.component.BJJPanel;
import com.bff.bjj.utils.Utils;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bff.slimserver.musicobjects.SlimPlayableItem;
import org.bff.slimserver.musicobjects.SlimPlayableObject;

/**
 *
 * @author  Bill
 */
public class SongInfoPanel
        extends BJJPanel implements MouseListener {

    private ThumbnailPanel thumbnailPanel;
    private boolean showArt;
    private boolean showComment;
    private SlimPlayableItem item;

    public SongInfoPanel() {
        this(null, false, false);
    }

    public SongInfoPanel(boolean showImageArt) {
        this(null, showImageArt, true);
    }

    public SongInfoPanel(SlimPlayableItem song, boolean showImageArt, boolean showComment) {
        setItem(song);

        initComponents();

        this.showArt = showImageArt;
        this.showComment = showComment;

        if (showImageArt) {
            thumbnailPanel = new ThumbnailPanel();
            thumbnailPanel.setPreferredSize(new Dimension(300, 300));
            thumbnailPanel.addMouseListener(this);
            thumbPanel.add(thumbnailPanel);
        } else {
            remove(thumbPanel);
            remove(songScrollPane);
            add(songScrollPane, BorderLayout.CENTER);
        }

        if (song != null) {
            validateFields(song);
            setSongInfo(song);
        }

        if(!JukeboxProperties.getInstance().isShowCoverArt()) {
            jPanel2.remove(jLabel2);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        thumbPanel = new BJJPanel();
        thumbPanel.setBackground(ColorProperties.PANEL_COVER_ART_BACKGROUND);
        jPanel2 = new BJJPanel();
        songScrollPane = new javax.swing.JScrollPane();
        jPanel1 = new BJJPanel();
        jLabel1 = new javax.swing.JLabel();
        lblArtist = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        lblAlbum = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        lblTitle = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        lblTrack = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        lblYear = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        lblBitrate = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        lblComment = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        lblType = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setForeground(new java.awt.Color(102, 102, 102));
        setLayout(new java.awt.BorderLayout());

        thumbPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        add(thumbPanel, java.awt.BorderLayout.CENTER);

        jPanel2.setOpaque(false);
        jPanel2.setLayout(new java.awt.BorderLayout());

        songScrollPane.setBorder(null);
        songScrollPane.setOpaque(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel1.setLayout(new java.awt.GridBagLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel1.setText("Artist:");
        jLabel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 5));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        jPanel1.add(jLabel1, gridBagConstraints);

        lblArtist.setForeground(new java.awt.Color(0, 102, 255));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        jPanel1.add(lblArtist, gridBagConstraints);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel3.setText("Album:");
        jLabel3.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 5));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        jPanel1.add(jLabel3, gridBagConstraints);

        lblAlbum.setForeground(new java.awt.Color(0, 102, 255));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        jPanel1.add(lblAlbum, gridBagConstraints);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel5.setText("Title:");
        jLabel5.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 5));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        jPanel1.add(jLabel5, gridBagConstraints);

        lblTitle.setForeground(new java.awt.Color(0, 102, 255));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        jPanel1.add(lblTitle, gridBagConstraints);

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel7.setText("Track:");
        jLabel7.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 5));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        jPanel1.add(jLabel7, gridBagConstraints);

        lblTrack.setForeground(new java.awt.Color(0, 102, 255));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        jPanel1.add(lblTrack, gridBagConstraints);

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel9.setText("Year:");
        jLabel9.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 5));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        jPanel1.add(jLabel9, gridBagConstraints);

        lblYear.setForeground(new java.awt.Color(0, 102, 255));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        jPanel1.add(lblYear, gridBagConstraints);

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel11.setText("Bitrate:");
        jLabel11.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 5));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        jPanel1.add(jLabel11, gridBagConstraints);

        lblBitrate.setForeground(new java.awt.Color(0, 102, 255));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        jPanel1.add(lblBitrate, gridBagConstraints);

        jLabel12.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel12.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel12.setText("Comment:");
        jLabel12.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 5));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        jPanel1.add(jLabel12, gridBagConstraints);

        lblComment.setForeground(new java.awt.Color(0, 102, 255));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        jPanel1.add(lblComment, gridBagConstraints);

        jLabel13.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel13.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel13.setText("Type:");
        jLabel13.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 5));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        jPanel1.add(jLabel13, gridBagConstraints);

        lblType.setForeground(new java.awt.Color(0, 102, 255));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        jPanel1.add(lblType, gridBagConstraints);

        songScrollPane.setViewportView(jPanel1);

        jPanel2.add(songScrollPane, java.awt.BorderLayout.CENTER);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Click to view full size");
        jPanel2.add(jLabel2, java.awt.BorderLayout.NORTH);

        add(jPanel2, java.awt.BorderLayout.SOUTH);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lblAlbum;
    private javax.swing.JLabel lblArtist;
    private javax.swing.JLabel lblBitrate;
    private javax.swing.JLabel lblComment;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JLabel lblTrack;
    private javax.swing.JLabel lblType;
    private javax.swing.JLabel lblYear;
    private javax.swing.JScrollPane songScrollPane;
    private javax.swing.JPanel thumbPanel;
    // End of variables declaration//GEN-END:variables

    /**
     * Pass null to remove all info
     * @param song
     */
    public void setSongInfo(SlimPlayableItem song) {
        try {
            if (song != null) {

                jLabel2.setVisible(true);

                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException ex) {
                            Logger.getLogger(SongInfoPanel.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        jLabel2.setVisible(false);
                    }
                }).start();

                setItem(song);
                
                validateFields(song);

                if (showArt) {
                    thumbnailPanel.loadImage(song);
                }
            } else {
                lblAlbum.setText("");
                lblArtist.setText("");
                lblTitle.setText("");
                lblTrack.setText("");

                jLabel12.setVisible(true);
                lblComment.setVisible(true);
                lblComment.setText("");

                setBitrate("");

                jLabel9.setVisible(true);
                lblYear.setVisible(true);
                lblYear.setText("");
                if (showArt) {
                    thumbnailPanel.removeImage();
                }
            }

        } catch (NullPointerException npe) {
            //dont give a shit
            npe.printStackTrace();
        }
    }

    public void setBitrate(String bitrate) {
        lblBitrate.setText(bitrate);
        /*
        if (bitrate < 0) {
        jLabel11.setVisible(false);
        lblBitrate.setVisible(false);
        } else {
        jLabel11.setVisible(true);
        lblBitrate.setVisible(true);
        }

        if (bitrate > 0) {
        jLabel11.setVisible(true);
        lblBitrate.setVisible(true);
        if (bitrate > 1000) {
        lblBitrate.setText(Integer.toString(bitrate / 1000) + " kpbs");
        } else {
        lblBitrate.setText(Integer.toString(bitrate) + " kbps");
        }
        }
         */

    }

    private void validateFields(SlimPlayableObject song) {

        lblTitle.setText(song.getTitle());

        if (!showComment) {
            jLabel12.setVisible(false);
            lblComment.setVisible(false);
        } else {
            if (song.getComment() == null || "".equals(song.getComment())) {
                jLabel12.setVisible(false);
                lblComment.setVisible(false);
            } else {
                jLabel12.setVisible(true);
                lblComment.setVisible(true);
                lblComment.setText(song.getComment());
            }

            if (song.getBitrate() == null || "".equals(song.getBitrate())) {
                jLabel11.setVisible(false);
                lblBitrate.setVisible(false);
            } else {
                jLabel11.setVisible(true);
                lblBitrate.setVisible(true);
                setBitrate(song.getBitrate());
            }
        }


        if (song.getArtist() == null || "".equals(song.getArtist())) {
            jLabel1.setVisible(false);
            lblArtist.setVisible(false);
        } else {
            jLabel1.setVisible(true);
            lblArtist.setVisible(true);
            lblArtist.setText(song.getArtist());
        }

        if (song.getAlbum() == null ||
                "".equals(song.getAlbum().getName()) ||
                song.getAlbum().getName() == null) {
            jLabel3.setVisible(false);
            lblAlbum.setVisible(false);
        } else {
            jLabel3.setVisible(true);
            lblAlbum.setVisible(true);
            lblAlbum.setText(song.getAlbum().getName());
        }

        if (song.getTrack() < 1) {
            jLabel7.setVisible(false);
            lblTrack.setVisible(false);
        } else {
            jLabel7.setVisible(true);
            lblTrack.setVisible(true);
            lblTrack.setText(Integer.toString(song.getTrack()));
        }

        if (song.getYear() == null || "".equals(song.getYear()) || "0".equals(song.getYear())) {
            jLabel9.setVisible(false);
            lblYear.setVisible(false);
        } else {
            jLabel9.setVisible(true);
            lblYear.setVisible(true);
            lblYear.setText(song.getYear());
        }

        if (song.isRemote() && song.getType() != null && !"".equalsIgnoreCase(song.getType())) {
            jLabel13.setVisible(true);
            lblType.setVisible(true);
            lblType.setText(song.getType());
        } else {
            jLabel13.setVisible(false);
            lblType.setVisible(false);
        }
    }

    /**
     * @return the item
     */
    public SlimPlayableItem getItem() {
        return item;
    }

    /**
     * @param item the item to set
     */
    public void setItem(SlimPlayableItem item) {
        this.item = item;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        CoverArtThumbnailDialog dialog = new CoverArtThumbnailDialog(Utils.getParentComponent(this), getItem());
        dialog.setVisible(true);
    }

    @Override
    public void mousePressed(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseExited(MouseEvent e) {
       // throw new UnsupportedOperationException("Not supported yet.");
    }
}
